package cn.hl.hm.logic.manage;

import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverVehicle;
import cn.hl.hm.entity.VehicleLength;
import cn.hl.hm.entity.VehicleType;
import cn.hl.hm.logic.service.IDriverService;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.DriverVehicleService;
import cn.hl.hm.service.VehicleLengthService;
import cn.hl.hm.service.VehicleTypeService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.List2Map;
import cn.hl.hm.util.paging.PageInfo;
import cn.hl.hm.vo.DriverDetailVo;
import cn.hl.hm.vo.VehicleVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 司机逻辑处理
 * @Author: xianghan
 * @Date: 2017/10/27 14:51
 **/
@Service
public class DriverManageLogic {
    private static final Logger logger = LoggerFactory.getLogger(DriverManageLogic.class);
    @Autowired
    IDriverService iDriverService;
    @Autowired
    DriverService driverService;
    @Autowired
    DriverVehicleService driverVehicleService;
    @Autowired
    VehicleTypeService vehicleTypeService;
    @Autowired
    VehicleLengthService vehicleLengthService;

    public void getDriverList(String name, String driverPhone, String driverStatus, String plateNumber,
                              Integer pageId, Integer pageCount, ModelMap modelMap) throws Exception {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCurrentResult(((pageId <= 0) ? 1 : pageId - 1) * pageCount);
        pageInfo.setPageSize(pageCount);
        List<DriverDetailVo> driverDetailVos = null;
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("driverPhone", driverPhone);
        if (driverStatus!=null && driverStatus.equals("0")) {//待审核的车辆列表
            params.put("plateNumber", plateNumber);
            pageInfo.setParams(params);
            List<Driver> driverList=iDriverService.selectListByIds(iDriverService.selectExistCheckPendingListPage(pageInfo));
            driverDetailVos=iDriverService.getDriverPageList(driverList);
        }else {
            params.put("driverStatus", driverStatus);
            if(plateNumber==null || plateNumber.length()==0){
                pageInfo.setParams(params);
                driverDetailVos = iDriverService.getDriverPageList(pageInfo);
            }else{
                params.put("plateNumber", plateNumber);
                pageInfo.setParams(params);
                List<Driver> driverList = iDriverService.selectListByIds(iDriverService.selectDriverListPage
                        (pageInfo));
                driverDetailVos=iDriverService.getDriverPageList(driverList);
            }
        }
        modelMap.addAttribute("pageInfo", pageInfo.build());
        modelMap.addAttribute("driverPageList",driverDetailVos);
        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
    }

    /**
     * 查询所有司机，用于导出
     * @return
     * @throws Exception
     */
    public List<DriverDetailVo> getDriverAll() throws Exception {
        List<DriverDetailVo> driverDetailVos=null;
        //所有司机
        List<Driver> driverList = driverService.selectDriver(new Driver());
        if (driverList!=null) {
            driverDetailVos=new ArrayList<>();
            List<DriverVehicle> driverVehicles = driverVehicleService.selectByDIVI(null, null);
            List<VehicleType> vehicleTypes=vehicleTypeService.selectVehicleType(new VehicleType());
            List<VehicleLength> vehicleLengths=vehicleLengthService.selectVehicleLength();
            List2Map list2Map = List2Map.getInstance();
            Map<Integer,List<DriverVehicle>> driverVehiclesMap = list2Map.tobeMaps("getDriverId", driverVehicles);
            Map<Integer,VehicleType> vehicleTypeMap= list2Map.tobeMap("getId",vehicleTypes);
            Map<Integer,VehicleLength> vehicleLengthMap= list2Map.tobeMap("getId",vehicleLengths);
            DriverDetailVo driverDetailVo=null;
            List<VehicleVo> vehicleVos=null;
            VehicleVo vehicleVo=null;
            VehicleType vehicleType=null;
            VehicleLength vehicleLength=null;
            List<DriverVehicle> vehicles=null;
            for (Driver driver : driverList) {
                driverDetailVo=new DriverDetailVo(driver);
                vehicles = driverVehiclesMap.get(driver.getId());
                if (list2Map.isNotEmpty(vehicles)) {
                    vehicleVos=new ArrayList<>();
                    for (DriverVehicle vehicle : vehicles) {
                        vehicleVo=new VehicleVo(vehicle);
                        vehicleType=vehicleTypeMap.get(vehicle.getVehicleTypeId());
                        if (vehicleType!=null) {
                            vehicleVo.setVehicleTpye(vehicleType.getName());
                            vehicleVo.setVehicleLoad(vehicleType.getVehicleLoad());
                        }
                        vehicleLength=vehicleLengthMap.get(vehicle.getVehicleLengthId());
                        if (vehicleLength!=null) {
                            vehicleVo.setVehicleLength(vehicleLength.getVehicleLength());
                        }
                        vehicleVos.add(vehicleVo);
                    }
                    driverDetailVo.setVehicleList(vehicleVos);
                }
                driverDetailVos.add(driverDetailVo);
            }
        }
        return driverDetailVos;
    }

    public void addDriver(Driver driver, String appId, ModelMap modelMap) {
        Driver param = new Driver();
        if (StringUtils.isNotBlank(appId)) {
            driver.setPlatformId(appId);
        }
        // 是否重复注册
        param.setDriverAccount(driver.getDriverPhone());
        List<Driver> selectDriver = driverService.selectDriver(param);
        if (selectDriver != null && selectDriver.size() > 0) {
            modelMap.addAttribute("infoCode", InfoCode.REGIST_ERROR);
        } else {
            if (iDriverService.addDriver(driver) != null) {
                modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
            } else {
                modelMap.addAttribute("infoCode", InfoCode.FAILURE);
            }
        }

    }

    public void updateDriver(Driver driver, ModelMap modelMap) throws Exception {
        if (iDriverService.updateDriver(driver) > 0) {
            modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
        } else {
            modelMap.addAttribute("infoCode", InfoCode.FAILURE);
        }
    }

    public void checkDriver(Integer driverId, String driverStatus, String driverUnpassReason, ModelMap
            modelMap) throws Exception {
        Driver driver = new Driver();
        driver.setId(driverId);
        driver.setDriverStatus(driverStatus);
        driver.setDriverUnpassReason(driverUnpassReason);
        int rows = driverService.updateByPrimaryKeySelective(driver);
        if (rows > 0) {
            iDriverService.driverCheckPush(driverStatus, driverService.selectByPrimaryKey(driverId));
            modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
        } else {
            modelMap.addAttribute("infoCode", InfoCode.FAILURE);
        }

    }

//    public void deleteDriver(Integer driverId, ModelMap modelMap) {
//        iDriverService.deleteDriver(driverId);
//        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
//    }

    public void getDriverDetail(Integer driverId,String driverPhone, ModelMap modelMap) {
        DriverDetailVo driverDetail = iDriverService.getDriverDetail(driverId,driverPhone);
        modelMap.addAttribute("driverDetail", driverDetail);
        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
    }


}
